package com.sjzl.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import com.sjzl.service.BaseService;
import com.sjzl.dao.BaseDao;
import com.sjzl.entity.BaseEntity;
@Transactional
public abstract class ServiceImpl<T extends BaseEntity> implements BaseService<T>{

	private BaseDao<T> dao;
	public BaseDao<T> getDao() {
		return dao;
	}
	@Resource(name="daoImpl")
	public void setDao(BaseDao<T> dao) {
		this.dao = dao;
	}
	
	public T get(Class<T> clazz, int id) {
		return this.dao.get(clazz, id);
	}

	public abstract void create(T entity);

	public void save(T entity) {
		this.dao.save(entity);
	}
	
	@Transactional
	public void delete(T entity) {
		this.dao.delete(entity);
	}

	@Transactional
	public void update(T entity) {
		this.dao.update(entity);
	}
	
	@Transactional
	public void saveOrUpdate(T entity) {
		this.dao.saveOrUpdate(entity);
	}
	
	public List<T> list(String hql){
		return this.dao.list(hql);
	}

	public List<T> list(String hql, Object... params) {
		return this.dao.list(hql, params);
	}

	public List<T> list(String hql, int firstResult, int maxResults,
			Object... params) {
		return this.dao.list(hql, firstResult, maxResults, params);
	}

	public int getTotalCount(String entityName, String where) {
		return this.getDao().getTotalCount(entityName, where);
	}
	
	public List<T> pageDate(String hql, int pageNo, int pageSize) {
		int firstResult = (pageNo - 1) * pageSize;
		return this.getDao().list(hql, firstResult, pageSize);
	}
	
	public List<T> pageDate(String hql, int pageNo, int pageSize, Object... params) {
		int firstResult = (pageNo - 1) * pageSize;
		return this.getDao().list(hql, firstResult, pageSize, params);
	}

}
